package com.sinping.qh.api.app.version.v1.records;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.app.records.HighLightByAppDTO;
import com.sinping.qh.service.app.records.IHighLightService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangjin
 * @create 2018/6/2
 */
@Api(value = "app亮点",description = "app亮点API")
@RestController
@RequestMapping(value =  Const.APP_URI_V1+"/light_point",name="亮点")
public class HighLightEndPointV1 extends BaseAppEndPoint {


    @Autowired
    IHighLightService iHighLightService;

    @ApiOperation(httpMethod = "POST", value = "添加亮点")
    @RequestMapping(name = "添加亮点", method = RequestMethod.POST)
    public ResDto addHighLight(@ApiParam(name="msg",value="{\"id\":\"\",\"check_id\":\"123\",\"desc\":\"testunit\","+
            "\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}]\n" +
            ",\"create_user_id\":\"123\",\"create_user_name\":\"zhj\"}")
                          @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HighLightByAppDTO highLightByAppDTO= JsonMapper.defaultMapper().fromJson(msg, HighLightByAppDTO.class);

        iHighLightService.save(highLightByAppDTO);

        return resDto;
    }

    /**
     * {\"id\":\"123\",\"desc\":\"testunit\","+
     "\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"update_user_id\":\"123\"}
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "PUT", value = "修改亮点")
    @RequestMapping(name = "修改亮点", method = RequestMethod.PUT)
    public ResDto updateHighLight(@ApiParam(name="msg",value="")
                             @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HighLightByAppDTO highLightByAppDTO=JsonMapper.INSTANCE.fromJson(msg, HighLightByAppDTO.class);
        highLightByAppDTO=iHighLightService.update(highLightByAppDTO);
        //转换返回的数据
        String data = JsonMapper.defaultMapper().toJson(highLightByAppDTO);
        resDto.setDatas(data);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiOperation(httpMethod = "DELETE", value = "删除亮点")
    @RequestMapping(value = "/{id}",name = "删除亮点", method = RequestMethod.DELETE)
    public ResDto deleteHighLight(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        boolean ret =iHighLightService.delete(id);

        if(ret)
        {
            resDto.setMsg(ConstEum.SUCCESS.getMessage());
        }
        else{
            resDto.setMsg(ConstEum.FAIL.getMessage());
        }

        return resDto;
    }
    @ApiParam(name = "id")
    @ApiOperation(httpMethod = "GET", value = "亮点明细")
    @RequestMapping(value = "/{id}",name = "亮点明细", method = RequestMethod.GET)
    public ResDto highLightDetail(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        HighLightByAppDTO highLightByAppDTO=  iHighLightService.findOne(id);

        String data=JsonMapper.defaultMapper().toJson(highLightByAppDTO);
        resDto.setDatas(data);

        return resDto;
    }


}
